var term;

function setupTerminal() {
  term = new Terminal({
    theme: {
      foreground: '#232322',
      background: '#eaeaea',
      cursor: '#16afca',

      black: '#212121',
      brightBlack: '#424242',

      red: '#b7141f',
      brightRed: '#e83b3f',

      green: '#457b24',
      brightGreen: '#7aba3a',

      yellow: '#f6981e',
      brightYellow: '#ffea2e',

      blue: '#134eb2',
      brightBlue: '#54a4f3',

      magenta: '#560088',
      brightMagenta: '#aa4dbc',

      cyan: '#0e717c',
      brightCyan: '#26bbd1',

      white: '#efefef',
      brightWhite: '#d9d9d9'
    }
  });
  const { master, slave } = openpty();
  term.loadAddon(master);

  // const fitAddon = new FitAddon();
  // term.loadAddon(fitAddon);

  // const searchAddon = new SearchAddon();
  // term.loadAddon(searchAddon);

  // const webglAddon = new WebglAddon();
  // webglAddon.onContextLoss(e => {
  //   addon.dispose();
  // });
  // term.loadAddon(webglAddon);

  term.open(document.getElementById('terminal'));
  // fitAddon.fit();
  // term.writeln('Hello from \x1B[1;3;31mxterm.js\x1B[0m $');
  // term.onData((val) => {
  //     term.write(val);
  // })
  slave.onReadable(() => {
    const input = (new TextDecoder).decode(Uint8Array.from(slave.read()));
    eval(input);
  });
  Wab.addMessageListener((event) => {
    console.log('Wab.addMessageListener', event)
    term.writeln(`${event.data}\n`);
  })
}

function invokeNativeFunction() {
  Wab.callFunction({
    name: 'PlatformVersion',
  })
}

function invokeNativeFunctionRely() {
  Wab.callFunction({
    name: 'heavy',
  }).then((v) => {
    console.log('invokeNativeFunctionRely', v)
    Wab.callFunction({ funcName: 'toast', 'paras': { 'msg': 'Roger That!', 'duration': 'long' } })
  }).catch((e) => {
    console.error('invokeNativeFunctionRely', e)
  })
}

function invokeNativeValue() {
  const completed = Wab.getValue({ name: 'completed' })
  console.log('invokeNativeValue', completed, JSON.parse(completed))
  term.writeln('invokeNativeValue' + completed + '\n')
}

function abilityCheck() {
  try {
    const v = Wab.getValue({ name: 'isSupportWebListener', preferChannel: 0 })
    if (!v) {
      console.warn('[abilityCheck][isSupportWebListener] no return!')
      return;
    }
    const value1 = JSON.parse(v)
    console.log('isSupportWebListener', v, value1)
    document.getElementById('webMessageListener').checked = value1.data.webMessageListener
    document.getElementById('postWebMessage').checked = value1.data.postWebMessage

    const v2 = Wab.getValue({ name: 'isSupportWebChannel', preferChannel: 0 })
    if (!v2) {
      console.warn('[abilityCheck][isSupportWebChannel] no return!')
      return;
    }
    const value2 = JSON.parse(v2)
    console.log('isSupportWebChannel', v2, value2)
    document.getElementById('createWebMessageChannel').checked = value2.data.createWebMessageChannel
    document.getElementById('webMessagePortSetMessageCallback').checked = value2.data.webMessagePortSetMessageCallback
    document.getElementById('webMessagePortClose').checked = value2.data.webMessagePortClose
    document.getElementById('webMessagePostMessage').checked = value2.data.webMessagePostMessage
    document.getElementById('postWebMessage2').checked = value2.data.postWebMessage
  } catch (error) {
    console.error('abilityCheck', error)
  }
}

window.addEventListener('DOMContentLoaded', () => {
  setupTerminal();
  eruda.init();
  abilityCheck()
})
